package com.gouaixin.bee.beeframework2.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ab.util.AbImageUtil;
import com.ab.util.AbToastUtil;
import com.gouaixin.bee.beeframework2.MyAPI;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.bean.GUANZHU;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.gouaixin.bee.beeframework2.bean.BaseModel;
import com.lidroid.xutils.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

import java.util.ArrayList;

/**
 * Created by Administrator on 2015/4/1.
 */
//我关注的
public class guanzhubieAdapter extends BaseAdapter {
    Context context;
    LayoutInflater mLayoutInflater;
    ArrayList<GUANZHU> data=new  ArrayList<GUANZHU>();
    ImageLoader imageLoader;

    public guanzhubieAdapter(Context context) {
        this.context = context;
        this.mLayoutInflater = LayoutInflater.from(context);
        this.imageLoader=ImageLoader.getInstance();
    }

    public void setData(ArrayList<GUANZHU> data) {
        this.data=data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
//        ViewHolder viewHolder;
//        if(convertView!=null){
            convertView=mLayoutInflater.inflate(R.layout.item_guanzhu2,null);
//            viewHolder = new ViewHolder();

//        }
        final ImageView icon= (ImageView) convertView.findViewById(R.id.icon);
        TextView name=(TextView)convertView.findViewById(R.id.name);
        TextView age=(TextView)convertView.findViewById(R.id.age);
        TextView photonumber=(TextView)convertView.findViewById(R.id.photonumber);
        final TextView guanzhu=(TextView)convertView.findViewById(R.id.isGuanzhu);

        final GUANZHU bean=data.get(position);


        imageLoader.loadImage(bean.getIcon(),new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String s, View view) {

            }

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {

            }

            @Override
            public void onLoadingComplete(String s, View view, Bitmap bitmap) {
//                imageLoader.displayImage(com.ytr.mymodule.app.bean.getIcon(),icon);
                icon.setBackground(AbImageUtil.bitmapToDrawable(bitmap));
            }

            @Override
            public void onLoadingCancelled(String s, View view) {

            }
        });



        name.setText(bean.getName());
        age.setText(bean.getAge());
        photonumber.setText(bean.getPhotonumber()+"照片");

        final String cid = PreferencesUtils.getString(context, "cid", "");
        guanzhu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseModel.setCallBackLister(new BaseModel.CallBackLister() {
                    @Override
                    public String onCallBack(String url, String result) {
                        AbToastUtil.showToast(context, "取消关注成功");
                        PreferencesUtils.putBoolean(context,bean.getId()+"isGuanzhu",false);
                        data.remove(position);
                        notifyDataSetChanged();
                        return url;
                    }
                });
                RequestParams params=new RequestParams();
                params.addBodyParameter("id",bean.getId());
                params.addBodyParameter("cid", cid);
                BaseModel.post(MyAPI.removeguanzhu,params);
            }
        });


        return convertView;
    }

    public class ViewHolder {
        ImageView icon;
        TextView name;
        TextView age;
        TextView photonumber;
        TextView guanzhu;
    }
}
